import random
import pygame
from pygame.sprite import Sprite
import constants


class MidEnemy(Sprite):
    """中型敌机类"""

    # 敌机的偏移量
    offset = 4
    # 爆炸动画播放频率计数器
    explode_animation_counter = 0
    # 打击动画播放频率计数器
    hit_animation_counter = 0

    def __init__(self, window):
        super().__init__()
        self.image = self.image_mid = pygame.image.load("images/mid_enemy.png")
        self.image_hit = pygame.image.load("images/mid_enemy_hit.png")
        self.image_explode1 = pygame.image.load("images/mid_enemy_explode1.png")
        self.image_explode2 = pygame.image.load("images/mid_enemy_explode2.png")
        self.image_explode3 = pygame.image.load("images/mid_enemy_explode3.png")
        self.image_explode4 = pygame.image.load("images/mid_enemy_explode4.png")
        self.window = window
        # 获取敌机的矩形
        self.rect = self.image.get_rect()
        # 获取window的矩形
        self.window_rect = self.window.get_rect()
        # 设置敌机的初始位置
        self.rect.bottom = self.window_rect.top
        self.rect.left = random.randint(0, self.window_rect.width - self.rect.width)
        # 是否在播放被击中动画
        self.is_playing_hit_animation = False
        # 是否在播放爆炸动画
        self.is_playing_explode_animation = False
        # 敌机的生命值
        self.life = constants.INITIAL_LIFE_OF_MID_ENEMY

    def update(self):
        """更新敌机"""
        self.rect.bottom += MidEnemy.offset

    @staticmethod
    def play_explode_sound():
        """播放敌机爆炸的声音"""
        # 加载爆炸的音频
        explode_sound = pygame.mixer.Sound("sounds/mid_enemy_explode.wav")
        # 设置音量
        explode_sound.set_volume(constants.EXPLODE_VOLUME)
        # 播放声音
        explode_sound.play()

    def start_explode_animation(self):
        """开始播放爆炸动画（切换图片）"""
        self.explode_animation_counter += 1
        if self.explode_animation_counter == constants.ANIM_FREQUENCY_OF_MID_ENEMY_EXPLODE:
            if self.image == self.image_mid:
                self.image = self.image_explode1
            elif self.image == self.image_explode1:
                self.image = self.image_explode2
            elif self.image == self.image_explode2:
                self.image = self.image_explode3
            elif self.image == self.image_explode3:
                self.image = self.image_explode4
            elif self.image == self.image_explode4:
                # 删除所有group中的该对象
                self.kill()
            # 重置计数器
            self.explode_animation_counter = 0

    def start_hit_animation(self):
        """开始播放被打击动画（切换图片）"""
        self.hit_animation_counter += 1
        if self.hit_animation_counter == constants.ANIM_FREQUENCY_OF_MID_ENEMY_HIT:
            if self.image == self.image_mid:
                self.image = self.image_hit
            elif self.image == self.image_hit:
                self.image = self.image_mid
                # 将标志位置为False
                self.is_playing_hit_animation = False
            # 重置计数器
            self.hit_animation_counter = 0

    def draw_life_line(self):
        """画敌机的生命线条"""
        # 先画一条白色线条打底
        pygame.draw.line(self.window, (255, 255, 255), (self.rect.left, self.rect.top),
                         (self.rect.right, self.rect.top), 2)
        # 剩余生命值占比
        left_life_ratio = self.life / constants.INITIAL_LIFE_OF_MID_ENEMY
        if left_life_ratio > 0:
            # 画出红色生命值
            pygame.draw.line(self.window, (255, 0, 0), (self.rect.left, self.rect.top),
                             (self.rect.left + self.rect.width * left_life_ratio, self.rect.top), 2)
